/**
 * 	this is the Login Panel
 */

package gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import Entities.UserEntity;

import common.ChatIF;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JPasswordField;

public class LoginPanel extends JPanel implements ChatIF{
	
	AcademicFrame frame;
	LoginPanel myLogin;
	MainWindow mWindow;
	private JTextField usernameField;
	int privilege[];//={1,1,1,1,1,1};
	String pid;
	private JPasswordField passwordField;
	private JButton btnNewButton;
	ArrayList<Object> message;
	UserEntity user;
	
	public LoginPanel(AcademicFrame frame) {
		super();
		this.frame=frame;
		this.setSize(300, 300);
		/////
		this.myLogin = this;
		usernameField = new JTextField();
		usernameField.setBounds(54, 80, 122, 20);
		add(usernameField);
		usernameField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(54, 136, 122, 20);
		add(passwordField);
	
		initialize();
		
	}
	private void initialize() {
		setLayout(null);
		btnNewButton = new JButton("Login");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				user = new UserEntity(usernameField.getText(), String.valueOf(passwordField.getPassword())); // create a new user entity
                message = new ArrayList<Object>();
				message.add("Login");
				message.add(user);
				
				//frame.myclient.sendMessageToServer(message);
				AcademicFrame.myclient.sendMessageToServer(message);


			}
		});
		btnNewButton.setBounds(72, 179, 89, 23);
		add(btnNewButton);
		
		JLabel lblUserName = new JLabel("Username");
		lblUserName.setBounds(54, 55, 86, 14);
		add(lblUserName);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(54, 111, 89, 14);
		add(lblPassword);
	}
	@Override
	public void display(Object message) {
		
		ArrayList<Object> answer = (ArrayList<Object>) message;
			
		
		if (((String)(answer.get(0))).equalsIgnoreCase("allowed"))
		{
			
			pid=(String)answer.get(2);
			AcademicFrame.userID = pid;
			privilege = (int[])answer.get(1);
			mWindow=new MainWindow(frame,myLogin,privilege,pid);
			frame.setContentPane(mWindow);
			frame.setSize(500, 500);
		}
		else
		if (((String)(answer.get(0))).equalsIgnoreCase("not allowed"))
		{
		Dialog box=	new Dialog("Bad username or password.");
		box.setSize(300, 200);
		box.setVisible(true);
		passwordField.setText(null);
		usernameField.setText(null);
		}
		if(((String)(answer.get(0))).equalsIgnoreCase("User is alerady Logged in !"))
		{
			Dialog box=	new Dialog("User is alerady Logged in !");
			box.setSize(300, 200);
			box.setVisible(true);
			passwordField.setText(null);
			usernameField.setText(null);
		}
	
		// TODO Auto-generated method stub
		
	}
}
